package com.meichis.yndms.vm;

import android.util.SparseArray;

import androidx.collection.ArrayMap;

import com.meichis.mcsappframework.utils.UniversalInterface;
import com.meichis.yndms.common.PictureUtils;
import com.meichis.yndms.http.IWebServiceCallback;
import com.meichis.yndms.model.Picture;
import com.meichis.yndms.repository.manager.UploadManager;
import com.meichis.yndms.room.AppDatabase;

import java.io.File;
import java.util.List;

public class UploadPresenter extends MyViewModel implements IWebServiceCallback {

    private ArrayMap<String, Picture> picsList;

    private SparseArray<String> guids;
    private int sequence = 11;

    private UniversalInterface<Void, String> messageCallBack;


    public UploadPresenter(UniversalInterface<Void, String> messageCallBack) {
        picsList = new ArrayMap<>();
        //单次   上传过和上传中的workList  picsList  udlist的key值
        guids = new SparseArray<>();
        this.messageCallBack = messageCallBack;
    }


    public synchronized void onStart() {
        upLoadPicture();
        if (complete()) {
            onDestory();
        }
    }


    private void upLoadPicture() {
        List<Picture> pictures = AppDatabase.getInstance().pictureDao().findUpLoadPicture();

        for (Picture picture : pictures) {
            String filepath = picture.getFilePath().startsWith("file:///") ? picture.getFilePath().replace("file:///", "") : picture.getFilePath();
            File file = new File(filepath);
            if (!file.exists()) {
                AppDatabase.getInstance().pictureDao().delete(picture);
                continue;
            }
            picture.setFilePath(filepath);
            if (picsList.get(picture.getGuid()) == null) {
                picsList.put(picture.getGuid(), picture);
            }
        }
        if (picsList.size() == 0) {
            return;
        }
        doUpPic();
    }


    private void doUpPic() {

        Picture m = picsList.valueAt(0);
        sequence++;
        switch (m.getRelateType()) {
            case 4://拜访照片
                if (m.getProcessCode() != null && !m.getProcessCode().equals("")) {
                    UploadManager.getInstance().UploadVisitWorkPicture(sequence, m,this);
                } else {
                    UploadManager.getInstance().UploadInspectPicture(sequence, m,this);
                }

                break;
            default:
        }
        guids.append(sequence, m.getGuid());

    }

    private boolean complete() {
        return picsList.size() == 0;
    }


    public ArrayMap<String, Picture> getPicsList() {
        return picsList;
    }

    private void onDestory() {
        clear();
        messageCallBack.todo("上传完毕");
    }

    private void clear() {
        picsList.clear();
        guids.clear();
    }

    @Override
    public void onSucceed(int sequence,Object resultPack, String ReturnInfo, int Return) {
        messageCallBack.todo("success");
        if (guids == null || guids.get(sequence) == null) {
            return;
        }
        String guid = guids.get(sequence);

        //Picture 上传
        if (picsList != null && picsList.get(guid) != null) {
            PictureUtils.deletePicture(picsList.get(guid));
            picsList.remove(guid);
            if (picsList.size() > 0) {
                doUpPic();
            }
        }

        if (complete()) {
            onDestory();
        }
    }

    @Override
    public void onFailure(int sequence,int errorCode, String error) {
        messageCallBack.todo("failure");
        if (errorCode == 0 || errorCode == -200 || errorCode == -100) {
            //网络未连接清除此次上传
            clear();
            messageCallBack.todo("网络连接失败");
            return;
        }
        if (guids == null || guids.get(sequence) == null) {
            return;
        }
        String guid = guids.get(sequence);

        //Picture 上传
        if (picsList != null && picsList.get(guid) != null) {
            picsList.get(guid).setIsUpload(9);
            picsList.get(guid).setUpError(error);
            AppDatabase.getInstance().pictureDao().update(picsList.get(guid));
            picsList.remove(guid);
            if (picsList.size() > 0) {
                doUpPic();
            }
        }

        if (complete()) {
            onDestory();
        }
    }

}
